package com.ruoyi.leases.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.homeowners.domain.Homeowners;
import com.ruoyi.homeowners.service.IHomeownersService;
import com.ruoyi.leases.domain.Leases;
import com.ruoyi.leases.service.ILeasesService;
import com.ruoyi.methods.service.IPaymentMethodsService;
import com.ruoyi.period.service.IRentalPeriodService;
import com.ruoyi.methods.domain.PaymentMethods;
import com.ruoyi.period.domain.RentalPeriod;
import com.ruoyi.tenants.domain.Tenants;
import com.ruoyi.tenants.service.ITenantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 租约管理Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/leases/leases")
public class LeasesController extends BaseController
{
    @Autowired
    private ILeasesService leasesService;

    @Autowired
    private IPaymentMethodsService paymentMethodsService;

    @Autowired
    private IRentalPeriodService rentalPeriodService;

    @Autowired
    private ITenantsService tenantsService;

    @Autowired
    private IHomeownersService homeownersService;

    /**
     * 查询租约管理列表
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:list')")
    @GetMapping("/list")
    public TableDataInfo list(Leases leases)
    {
        startPage();
        List<Leases> list = leasesService.selectLeasesList(leases);
        return getDataTable(list);
    }

    /**
     * 导出租约管理列表
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:export')")
    @Log(title = "租约管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Leases leases)
    {
        List<Leases> list = leasesService.selectLeasesList(leases);
        ExcelUtil<Leases> util = new ExcelUtil<Leases>(Leases.class);
        util.exportExcel(response, list, "租约管理数据");
    }

    /**
     * 获取租约管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:query')")
    @GetMapping(value = "/{leaseId}")
    public AjaxResult getInfo(@PathVariable("leaseId") Long leaseId)
    {
        return success(leasesService.selectLeasesByLeaseId(leaseId));
    }

    /**
     * 新增租约管理
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:add')")
    @Log(title = "租约管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Leases leases)
    {
        return toAjax(leasesService.insertLeases(leases));
    }

    /**
     * 修改租约管理
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:edit')")
    @Log(title = "租约管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Leases leases)
    {
        return toAjax(leasesService.updateLeases(leases));
    }

    /**
     * 删除租约管理
     */
    @PreAuthorize("@ss.hasPermi('leases:leases:remove')")
    @Log(title = "租约管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leaseIds}")
    public AjaxResult remove(@PathVariable Long[] leaseIds)
    {
        return toAjax(leasesService.deleteLeasesByLeaseIds(leaseIds));
    }

    /**
     * 获取支付方式选项列表
     */
    @GetMapping("/paymentMethods")
    public AjaxResult getPaymentMethods()
    {
        List<PaymentMethods> paymentMethods = paymentMethodsService.selectPaymentMethodsList(new PaymentMethods());
        return AjaxResult.success(paymentMethods);
    }

    /**
     * 获取可选租期选项列表
     */
    @GetMapping("/rentalPeriods")
    public AjaxResult getRentalPeriods()
    {
        List<RentalPeriod> rentalPeriods = rentalPeriodService.selectRentalPeriodList(new RentalPeriod());
        return AjaxResult.success(rentalPeriods);
    }

    /**
     * 获取租客选项列表
     */
    @GetMapping("/tenants")
    public AjaxResult getTenants()
    {
        List<Tenants> tenants = tenantsService.selectTenantsList(new Tenants());
        return AjaxResult.success(tenants);
    }

    /**
     * 获取户主选项列表
     */
    @GetMapping("/homeowners")
    public AjaxResult getHomeowners()
    {
        List<Homeowners> homeowners = homeownersService.selectHomeownersList(new Homeowners());
        return AjaxResult.success(homeowners);
    }
}
